package com.family.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MybatisConfig {

    /**
     * 对应这一段标签配置
     * <typeAliases>
     *     <package name="com.family.domain"/>
     * </typeAliases>
     *
     * <environments default="mysql">
     *         <environment id="mysql">
     *             <transactionManager type="JDBC"></transactionManager>
     *             <dataSource type="POOLED">
     *                 <property name="driver" value="${jdbc.driver}"></property>
     *                 <property name="url" value="${jdbc.url}"></property>
     *                 <property name="username" value="${jdbc.username}"></property>
     *                 <property name="password" value="${jdbc.password}"></property>
     *             </dataSource>
     *         </environment>
     * </environments>
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setTypeAliasesPackage("com.family.domain");
        ssfb.setDataSource(dataSource);
        return ssfb;
    }

    /**
     * 对应这个标签
     * <mappers>
     *     <package name="com.family.dao"></package>
     * </mappers>
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.family.dao");
        return msc;
    }
}



